package com.enterprise.application.swing.jeta;

import com.enterprise.support.utility.Enterprise;
import com.enterprise.support.utility.Validation;
import com.jeta.forms.components.panel.FormPanel;
import com.jeta.open.support.DefaultComponentFinder;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-1-25
 * Time: 13:13:36
 * Swing JPanel 基类
 */
public class AbstractPanel<T extends Enterprise> extends JPanel {
    protected FormPanel mainPanel;
    private JPanel superPanel;
    private Enterprise input;
    public T model;

    public AbstractPanel(String viewCfgFileName) {
        if (viewCfgFileName == null)
            throw new IllegalArgumentException("Configration file is NULL !");
        setLayout(new BorderLayout());
        add(mainPanel = new FormPanel(viewCfgFileName) {
            public HashMap<String, WeakReference<Container>> getM_components() {
                return ((DefaultComponentFinder) getComponentFinder()).getM_components();
            }
        }, BorderLayout.CENTER);
        initComponents();
        addListeners();
    }

    public AbstractPanel(String viewCfgFileName, T model) {
        this(viewCfgFileName);
        this.model = model;
    }

    public AbstractPanel(String viewCfgFileName, JPanel superPanel, T model) {
        this(viewCfgFileName);
        this.superPanel = superPanel;
        this.model = model;
    }

    /**
     * 初始化组建
     */
    public void initComponents() {
    }

    /**
     * 注册事件
     */
    public void addListeners() {
    }

    public interface InputFilter {
        Enterprise filter(Enterprise input);
    }

    /**
     * 获取界面输入参数
     */
    public Enterprise getInput(InputFilter... filter) {
        if (null == input) {
            input = new Enterprise();
        }
        Map<String, WeakReference<Container>> components = mainPanel.getM_components();
        for (Map.Entry<String, WeakReference<Container>> component : components.entrySet()) {
            Object container = component.getValue().get();
            Object value = null;
            if (container instanceof JComponent) {
                if (container instanceof JTextComponent) {
                    value = ((JTextComponent) container).getText();
                } else if (container instanceof JComboBox) {
                    value = ((JComboBox) container).getSelectedItem();
                } else {
                    value = container;
                }
            } else if (container instanceof Component) {
                if (container instanceof TextField) {
                    value = ((TextField) container).getText();
                } else {
                    value = container;
                }
            }
            input.put(component.getKey(), value);
        }

        if (!Validation.isEmpty(filter) && filter.length > 0) {
            input = filter[0].filter(input);
        }
        return input;
    }

    /**
     * 数据模型
     *
     * @return
     */
    public T getModel() {
        return model;
    }

    /**
     * 提交验证
     */
    public boolean validation() {
        return true;
    }

    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame();
        frame.setPreferredSize(new Dimension(800, 600));
        frame.setLocation(100, 100);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(new AbstractPanel("d://test.xml"), BorderLayout.CENTER);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
